package com.linzp.bishe.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linzp.bishe.entity.Participants;
import com.linzp.bishe.mapper.ParticipantsMapper;
import com.linzp.bishe.service.ParticipantsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 林泽鹏
 * @since 2021-11-01
 */
@Service
public class ParticipantsServiceImpl extends ServiceImpl<ParticipantsMapper, Participants> implements ParticipantsService {

	@Autowired
	private ParticipantsMapper participantsMapper;
	@Override
	public boolean updateNameByUsersId(Integer id, String name) {
		return participantsMapper.updateNameByUsersId(id,name);
	}

	@Override
	public List<Participants> getListOfParticipants(Integer fk, String tableName) {
		return participantsMapper.getListOfParticipants(fk,tableName);
	}

	/**
	 * @Description 删除List
	 * @Param [list]
	 * @return void
	 */
	@Override
	public void removeListById(List<Participants> list) {
		List<Integer> deletedId = new ArrayList<>();
		for (Participants participants : list) {
			deletedId.add(participants.getId());
		}
		this.removeByIds(deletedId);
	}



	/**
	 * @Description List<Participants>
	 * @Param [list, fk, tableName]
	 * @return java.util.List<com.linzp.bishe.entity.Participants>
	 */
	@Override
	public void saveListOfParticipants(List<Participants> list, Integer fk, String tableName) {
		for (Participants participants : list) {
			participants.setResearchType(tableName);
			participants.setResearchFk(fk);
		}
		this.saveBatch(list);
	}

	/**
	 * @Description 通过逻辑外键与逻辑表修改参与人数
	 * @Param [list, tableName, fkId]
	 * @return void
	 */
	@Override
	public void dealParticipants(List<Participants> list, String tableName,Integer fkId) {
		List<Participants> participants = participantsMapper.getListOfParticipants(fkId,tableName);
		List<Participants> add = new ArrayList<>();
		List<Participants> updata = new ArrayList<>();
		for (Participants par : list) {
			//如果id为空就添加
			if (par.getId() != null) {
				for (int i = 0; i < participants.size(); i++) {
					if (participants.get(i).getId().equals(par.getId())) {
						if ((par.getParticipantName() != null & Objects.equals(participants.get(i).getParticipantName(),par.getParticipationMode()))|
								(par.getParticipationMode() != null & Objects.equals(participants.get(i).getParticipationMode(),par.getParticipationMode()))|
								(par.getSortNumber()) != null & Objects.equals(participants.get(i).getSortNumber(),par.getSortNumber())|
								(par.getWorkPlace() != null & Objects.equals(participants.get(i).getWorkPlace(),par.getWorkPlace()))|
								(par.getCategory() != null & Objects.equals(participants.get(i).getCategory(),par.getCategory()))){
							updata.add(par);
						}
						participants.remove(i);
						break;
					}
				}
			} else {
				par.setResearchType(tableName);
				par.setResearchFk(fkId);
				add.add(par);
			}
		}
		if(updata.size() > 0){
			this.updateBatchById(updata);
		}
		if(participants.size() > 0) {
			List<Integer> deletedIds = new ArrayList<>();
			for (Participants participant : participants) {
				deletedIds.add(participant.getId());
			}
			this.removeByIds(deletedIds);
		}
		if(add.size() > 0) {
			this.saveBatch(add);
		}
	}

	@Override
	public Integer getCountParticipants(Integer fk, String tableName) {
		return participantsMapper.getCountParticipants(fk, tableName);
	}
}
